/**
 * 消息列表
 *
 */
define(['app', 'echarts', 'walden', 'comm/pager/PagerControl', 'js/services/MessageBox', 'js/services/MuseService',
    'js/directives/DatetimePicker', 'js/controllers/common/modal/messagelog/MessageLogModalController',
    'js/filters/StrLimitFilter'], function (app, echarts) {

    var deps = ['$scope', '$state', '$modal', 'MessageBox', 'MuseService'];

    function controller ($scope, $state, $modal, MessageBox, MuseService) {
        //初始化查询对象 和 分页对象
        $scope.queryObj = {
            'sort': 'desc',
            'isHasRobot': false//是否包含机器人
        };
        $scope.pagerObj = {
            //当前页号
            currentPage: 1,
            // 每页显示条数
            pageSize: 15,
            //总记录数
            totalCount: -1
        };

        //获取列表
        $scope.getObjList = function () {
            //封装查询条件
            $scope.pagerObj.searchMap = $scope.queryObj;
            MuseService.post('admin/dataAnalyse/datanum.json', $scope.pagerObj).success(
                function (data, status, headers, config) {
//console.log(data);             	
                    if (data.state == 0) {
                        $scope.dataList = data.data;
                        var drawi = [];
                        var drawx = []; //x柱值
                        var drawdata = [];//T1_t会话总数
                        var drawudata = [];//会话人数
                        var drawvdata = [];//解决率
                        if (data.data.drawData) {
                            for (var i = 1; i < data.data.drawData.length; i++) {
                                var t = data.data.drawData[i]['T1_t'];//T1_t会话总数
                                var u = data.data.drawData[i]['T1_u'];//T1_u//会话人数
                                var v = data.data.drawData[i]['T1_v'];// T1_v 解决率
                                drawdata.push(t);
                                drawudata.push(u);
                                drawvdata.push(v);
                                drawx.push(i + ":00");
                                drawi.push(i);
                            }
                        }
                        createEcharts(drawx, drawdata, drawudata, drawvdata, drawi);
                    } else {
                        MessageBox.notify(data.message);
                    }
                });
        };

        $scope.getObjList();

        function createEcharts (drawx, drawdata, drawudata, drawvdata, drawi) {
            //组装数据
            var ret = getObjWidthHeight("charts");
            var obj = document.getElementById('mainCharts');
            obj.style.width = (ret["width"] - 20) + "px";
            obj.style.height = (ret["height"] - 10) + "px";
            var myChart = echarts.init(obj, 'walden');
            var option = {
                title: {
                    text: '今日会话',
                    //subtext: '纯属虚构'
                },
                tooltip: {
                    trigger: 'axis',
                    formatter: function (param) {
                        indexi = param[0].dataIndex;
                        var relVal = drawi[indexi] - 1 + ":00-" + param[0]['name'] + "<br/>";
                        relVal += "会话人数:" + drawudata[indexi] + '人<br/>';
                        relVal += "提问总数:" + param[0]['value'] + '<br/>';
                        relVal += "解决率:" + drawvdata[indexi] + '%';
                        return relVal;
                    }
                },
                legend: {
                    //data:['蒸发量']
                },
                toolbox: {
                    show: false,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        data: drawx
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '会话人数',
                        type: 'bar',
                        data: drawdata,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    }
                ]
            };


            myChart.setOption(option);
        }

    }

    controller.$inject = deps;
    app.lazy.controller('DataAnalyseController', controller);
});
